package com.qipay.admin;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     *
     */
    @Autowired
    UserDetailsService userService;

    @Override
    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/health", "/resource/**", "/resource/upload",
//                "/qrcode/**", "/account/register/**", "/account/login/**");
        web.ignoring()
                .antMatchers("/resource/**", "/static/**", "/resource/static/**",
                        "/login", "/login/**", "/islogin",
                        "/test/**");
    }

    /**
     * 安全过滤器，登陆失效，登陆之后，继续跳转原来的页面
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().xssProtection();

        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/index", "/index.jsp", "/order/**", "/doc/**", "/user/**",
                        "/cp/**", "/statement/**", "/sp/**", "/proxy/**", "/sys/**",
                        "/ajax/**", "/withdraw/**", "/notice/**", "/auth/**", "/data/**",
                        "/ips/**").authenticated()
                .anyRequest().permitAll()
                .and()
                .formLogin().loginPage("/login").defaultSuccessUrl("/")
                .usernameParameter("username").passwordParameter("password")
                .permitAll()
                .and()
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login")
                .invalidateHttpSession(true).permitAll().and();

        http.sessionManagement().sessionFixation().changeSessionId().maximumSessions(1024).expiredUrl("/");

        http.rememberMe().rememberMeParameter("remmeberMe").key("password");
        http.headers().frameOptions().sameOrigin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder);
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public AuthenticationFailureHandler authenticationFailureHandler() {
        return new SimpleUrlAuthenticationFailureHandler();
    }

}